﻿//
// Les chaines de texte des exceptions ne sont pas localisées car elles sont à destination des administrateurs.
//
using System;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using FranceTriple.Dal;

namespace TeamFranceTriple
{
    public partial class activation : BasePage
    {
        private static String prefixTitle = "Team France Triple" + " - ";
        private String ActivationSuccessLocalized = "ActivationSuccess".ToLocalizedString();

        Guid userKey;
        MembershipUser userToActivate = null;
        ContentPlaceHolder bodyContent;
        ActivationSuccess activationSuccess;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Page.RouteData.Values.Count == 0)
                    throw new HttpException(403, "(Routing Error) Invalid user account activation attempt with no key.");

                userKey = Guid.Parse(Page.RouteData.Values["activationKey"].ToString());
                if (userKey != null)
                    userToActivate = Membership.GetUser(userKey, true);
                else
                    throw new HttpException(403, "(ActivationKey is null) Invalid user account activation attempt for key : " + userKey.ToString().TextToHtml());

                if (userToActivate == null)
                    throw new HttpException(403, "(Unknown user) Invalid user account activation attempt for key : " + userKey.ToString().TextToHtml());

                // Test pour éviter le scénario suivant : un utilisateur est désactivé par un administrateur et se réactive tout seul en réutilisant l'email.
                // Du coup le lien d'activation de l'email ne marchera que la première fois.
                if (userToActivate.Comment != null && userToActivate.Comment.Contains(userToActivate.ProviderUserKey.ToString()))
                {
                    userToActivate.IsApproved = true;
                    userToActivate.Comment = userToActivate.Comment.Replace(userToActivate.ProviderUserKey.ToString(), "");
                    Membership.UpdateUser(userToActivate);
                }
                else
                    throw new HttpException(403, "(Not first time activation) : Invalid user account activation attempt for key : " + userKey.ToString().TextToHtml());
            }
            activationSuccess = LoadControl("~/Account/ActivationSuccess.ascx") as ActivationSuccess;
            bodyContent = ThisMaster.FindControl("MainContent") as ContentPlaceHolder;
            bodyContent.Controls.Add(activationSuccess);
            Page.Title = prefixTitle + ActivationSuccessLocalized;
        }
    }
}